package com.hi.hiparty.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;

import com.hi.hiparty.dao.query.PartyDBQuery;
import com.hi.hiparty.entity.Party;
import com.hi.hiparty.jdbc.SqlMapClientTemplateCache;
import com.hi.hiparty.util.IDGenerator;

/**
 * 方便单元测试，到时再切换统一数据源
 * 
 */
@Repository
public class PartyDao {

	protected SqlMapClientTemplate getSqlMapClientTemplate() throws Exception {
		return SqlMapClientTemplateCache.getSqlMapClientTemplate("hiparty");
	}

	public Party insert(Party party) throws Exception {
		String partyid = IDGenerator.generatePartyid();
		party.setPartyid(partyid);
		this.getSqlMapClientTemplate().insert("Party.insert", party);
		return party;
	}

	@SuppressWarnings("unchecked")
	public List<Party> getPartyByUserId(Integer userid) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userid", userid);
		return (List<Party>) this.getSqlMapClientTemplate().queryForList(
				"Party.getPartyByUserId", params);
	}

	// Party.getPartyById
	public Party getPartyById(String id) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		return (Party) this.getSqlMapClientTemplate().queryForObject(
				"Party.getPartyById", params);
	}

	public int delPartyByUserId(String userid) throws Exception {
		Map<String, String> params = new HashMap<String, String>();
		params.put("userid", userid);
		return this.getSqlMapClientTemplate().delete("Party.delPartyByUserId",
				params);
	}

	/**
	 * 
	 * @param userid
	 * @param lon
	 *            经度
	 * @param lat
	 *            纬度
	 * @return
	 * @throws Exception
	 */
	public int updateUserPosByUserId(int userid, double lon, double lat)
			throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userid", userid);
		params.put("userPos", lon + "," + lat);
		return this.getSqlMapClientTemplate().update(
				"Party.updateUserPosByUserId", params);
	}

	public int updateParty(Party party) throws Exception {
		return this.getSqlMapClientTemplate()
				.update("Party.updateParty", party);
	}

	@SuppressWarnings("unchecked")
	public List<Party> getPartysForQuery(PartyDBQuery q) throws Exception {
		return (List<Party>) this.getSqlMapClientTemplate().queryForList(
				"Party.getPartysForQuery", q);
	}

	public Integer getPartysCountForQuery(PartyDBQuery q) throws Exception {
		return (Integer) this.getSqlMapClientTemplate().queryForObject(
				"Party.getPartysCountForQuery", q);
	}

	@SuppressWarnings("unchecked")
	/**
	 * 非分页查询
	 */
	public List<Party> getPartysNoPageForQuery(PartyDBQuery q) throws Exception {
		return (List<Party>) this.getSqlMapClientTemplate().queryForList(
				"Party.getPartysNoPageForQuery", q);
	}
	
	public List<Party> getPartysByTime(int start, int size) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("start", start);
		params.put("size", size);
		return (List<Party>) this.getSqlMapClientTemplate().queryForList(
				"Party.getPartysByTime", params);
	}

}
